﻿<helpers:BindablePage x:Name="bindablePage"
    x:Class="BalanceManager.Views.HistoryView"
                      DataContext="{Binding Source={StaticResource ViewModelLocator}, Path=HistoryViewModel}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:viewModels="clr-namespace:BalanceManager.ViewModels"
    xmlns:uiControls="clr-namespace:BalanceManager.UIControls"
    xmlns:helpers="clr-namespace:BalanceManager.Helpers"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    xmlns:converters="clr-namespace:BalanceManager.Helpers.Converters"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True"
    BackKeyPress="BindablePage_BackKeyPress" >

    <!-- Animated page transition -->
    <toolkit:TransitionService.NavigationInTransition>
        <toolkit:NavigationInTransition>
            <toolkit:NavigationInTransition.Backward>
                <toolkit:TurnstileTransition Mode="BackwardIn" />
            </toolkit:NavigationInTransition.Backward>
            <toolkit:NavigationInTransition.Forward>
                <toolkit:TurnstileTransition Mode="ForwardIn" />
            </toolkit:NavigationInTransition.Forward>
        </toolkit:NavigationInTransition>
    </toolkit:TransitionService.NavigationInTransition>
    <toolkit:TransitionService.NavigationOutTransition>
        <toolkit:NavigationOutTransition>
            <toolkit:NavigationOutTransition.Backward>
                <toolkit:TurnstileTransition Mode="BackwardOut" />
            </toolkit:NavigationOutTransition.Backward>
            <toolkit:NavigationOutTransition.Forward>
                <toolkit:TurnstileTransition Mode="ForwardOut" />
            </toolkit:NavigationOutTransition.Forward>
        </toolkit:NavigationOutTransition>
    </toolkit:TransitionService.NavigationOutTransition>

    <helpers:BindablePage.Resources>
        <converters:EnumToStringConverter x:Key="EnumToStringConverter" />
            
        <DataTemplate x:Key="ListPickerDataTemplate">
            <Grid>
                <TextBlock Text="{Binding Converter={StaticResource EnumToStringConverter}}"/>
            </Grid>
        </DataTemplate>
    </helpers:BindablePage.Resources>

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel Grid.Row="0" Margin="12,17,0,0">
            <TextBlock Text="МЕНЕДЖЕР" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock Text="История" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid Grid.Row="0">
                <StackPanel>
                    <StackPanel>
                        <toolkit:ListPicker x:Name="periodList"
                                            Header="Период"
                                            ItemTemplate="{StaticResource ListPickerDataTemplate}"
                                            SelectedItem="{Binding Period, Mode=TwoWay}"
                                            SelectionMode="Single"/>
                    </StackPanel>
                </StackPanel>
            </Grid>
            <uiControls:ExtendedLongListSelector Grid.Row="1" ItemsSource="{Binding ItemsTree}" x:Name="treeListControl" Tap="TreeListControl_OnTap">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>

                            <TextBlock Grid.Column="0" Grid.Row="0" TextAlignment="Center" Style="{StaticResource PhoneTextLargeStyle}" Text="{Binding Item.ItemName}" />
                            <Grid Grid.Row="1">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="5*" />
                                    <ColumnDefinition Width="5*" />
                                </Grid.ColumnDefinitions>
                                <ProgressBar Grid.Column="0" 
                                         IsIndeterminate="False"
                                         Minimum="0"
                                         Maximum="{Binding DataContext.TotalExpensesAmount, ElementName=bindablePage}"
                                         Value="{Binding Item.ExpensesAmount}"
                                         FlowDirection="LeftToRight"
                                         Foreground="Maroon" />
                                <ProgressBar Grid.Column="1" 
                                         IsIndeterminate="False"
                                         Minimum="0"
                                         Maximum="{Binding DataContext.TotalProfitsAmount, ElementName=bindablePage}"
                                         Value="{Binding Item.ProfitsAmount}"
                                         FlowDirection="RightToLeft"
                                         Foreground="MidnightBlue" />
                            </Grid>
                            <TextBlock Grid.Row="2" TextAlignment="Left" Margin="10,0,0,0" Text="{Binding Item.ExpensesAmount, StringFormat=F2}" />
                            <TextBlock Grid.Row="2" TextAlignment="Right" Margin="0,0,10,0" Text="{Binding Item.ProfitsAmount, StringFormat=F2}" />

                        </Grid>

                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </uiControls:ExtendedLongListSelector>
        </Grid>
    </Grid>

</helpers:BindablePage>